package com.shanzhu.volunteer.common.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.shanzhu.volunteer.modules.system.entity.model.SysDept;
import com.shanzhu.volunteer.modules.system.entity.model.SysMenu;
import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * TreeSelect树结构实体类
 *
 * @author: ShanZhu
 * @date: 2023-02-26 19:39:54
 */
@Data
public class TreeSelect implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 节点ID
     */
    private Long id;

    /**
     * 节点名称
     */
    private String label;

    /**
     * 用户或者管理员（0是用户，1是管理员）
     */
    private String userOrAdmin;

    /**
     * 子节点
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<TreeSelect> children;

    public TreeSelect() {

    }

    public TreeSelect(SysDept dept) {
        this.id = dept.getDeptId();
        this.label = dept.getDeptName();
        this.children = dept.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    public TreeSelect(SysMenu menu) {
        this.id = menu.getMenuId();
        StringBuilder sb = new StringBuilder();
        sb.append(menu.getMenuName()).append(" ").append(menu.getUserOrAdmin().equals("0") ? "(用户)" : "(管理员)");
        this.label = sb.toString();
//        this.label = "" + menu.getMenuName() + (menu.getUserOrAdmin().equals("0") ? "(用户)" : "(管理员)");
        this.userOrAdmin = menu.getUserOrAdmin();
        this.children = menu.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList());
    }

}
